<?php


namespace app\admin\controller;


use app\constant\ActivesConst;
use app\constant\StoresConst;
use app\helper\ActiveHelper;
use app\model\AdminActives;
use app\model\AdminActiveSku;
use app\model\AdminStores;
use app\model\AdminVip;
use app\util\ReturnCode;
use think\Db;


class Vip extends Base
{
    /**
     * 获取数据
     */
    public function get()
    {
        $model = AdminVip::get($this->getUserTid());
        if (!$model) {
            // 启动事务
            Db::startTrans();
            try {
                //创建一个关联的产品
                $store = AdminStores::get($this->addTid(['level'=>StoresConst::LEVEL_SELF]));

                $createModel = AdminActives::create(['title' => 'VIP会员',
                    'start_time' => (new \DateTime())->format('Y-m-d H:i:s'),
                    'end_time' => (new \DateTime('2100-01-01 00:00:00'))->format('Y-m-d H:i:s'),
                    'type' => ActivesConst::ACTIVE_TYPE_VIP,
                    'sid' => $store->id,
                    'banner' => null,
                    'pay_type' => ActivesConst::PAY_TYPE_ONLINE,
                    'status' => ActivesConst::STATUS_NORMAL,
                    'tid' => $this->getUserTid(),
                    'refund_percentage' => 100
                ]);
                $model = AdminVip::create(['id' => $this->getUserTid(), 'goods_id' => $createModel->id]);
                // 提交事务
                Db::commit();

            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return $this->buildFailed(ReturnCode::FAILED, '初始化失败');
            }
        }
        $model = AdminVip::get($this->getUserTid());
        $data =$model->getData();
        $where = ['aid'=>$model->goods_id,'del'=>0];
        $skus = AdminActiveSku::all($where);
        $data['sku'] = $skus;
        return $this->buildSuccess($data);
    }

    public function save()
    {
        $postData = $this->request->post();
        unset($postData['id']);
        if ($postData['banner'] != null) {
            $postData['banner'] = json_encode($postData['banner']);
        }
        $model = AdminVip::get($this->getUserTid());
        if(!$model){
            return $this->buildFailed(ReturnCode::FAILED, '会员设置数据不存在');
        }
        $goodsModel = AdminActives::get($model->goods_id);
        if(!$goodsModel){
            return $this->buildFailed(ReturnCode::FAILED, '会员套餐数据不存在');
        }
        //构造VIP会员的产品数据
        $postData['goods_data']=$goodsModel->getData();

        $postData['goods_data']['banners']=$postData['banner'];
        $skus = $postData['sku'];;
        $postData['goods_data']['sku']=$skus;
        $done = ActiveHelper::save($postData['goods_data']);
        if ($done['done']) {
            $result = AdminVip::update(['banner' => $postData['banner'],'name'=>$postData['name'], 'top_color' => $postData['top_color']], ['id' => $this->getUserTid()]);
            if ($result) {
                return $this->buildSuccess(null, '操作成功');
            } else {
                return $this->buildFailed(ReturnCode::FAILED, '操作失败');
            }
        } else {
            return $this->buildFailed(ReturnCode::FAILED, $done['msg']);
        }
    }
}